package com.acwing.partition13;

import java.io.*;
import java.util.Arrays;

/**
 * @author `RKC`
 * @date 2022/3/9 10:48
 */
public class AC1234倍数问题_dp {

    private static final int N = 100010, K = 1010, INF = 0x3f3f3f3f;
    private static int[] nums = new int[N];
    private static int[][][] f;
    private static int n, k;

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        String[] ss = reader.readLine().split(" ");
        n = Integer.parseInt(ss[0]);
        k = Integer.parseInt(ss[1]);
        ss = reader.readLine().split(" ");
        for (int i = 1; i <= n; i++) nums[i] = Integer.parseInt(ss[i - 1]);
        f = new int[n + 1][4][K];
        for (int i = 0; i < f.length; i++) {
            for (int j = 0; j < f[i].length; j++) Arrays.fill(f[i][j], -INF);
            f[i][0][0] = 0;
        }
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= 3; j++) {
                for (int x = 0; x < k; x++) {
                    f[i][j][x] = Math.max(f[i - 1][j][x], f[i - 1][j - 1][getMod(x - nums[i], k)] + nums[i]);
                }
            }
        }
        writer.write(f[n][3][0] + "\n");
        writer.flush();
    }

    private static int getMod(int a, int b) {
        return (a % b + b) % b;
    }
}
